package com.ruoyi.mdkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.mdkt.entity.Course;
import com.ruoyi.mdkt.entity.ScoreLog;
import com.ruoyi.mdkt.entity.Student;
import com.ruoyi.mdkt.mapper.TeacherMapper;
import com.ruoyi.mdkt.service.CourseService;
import com.ruoyi.mdkt.service.ScoreLogService;
import com.ruoyi.mdkt.service.StudentService;
import com.ruoyi.mdkt.service.TeacherService;
import com.ruoyi.mdkt.utils.RankingUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gwl
 * @since 2022-10-22
 */
@CrossOrigin

@RestController
@RequestMapping("/mdkt/teacher")
public class TeacherController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ScoreLogService scoreLogService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private RankingUtils rankingUtils;
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/selectCourseDate/{courserId}")
    public R selectCourseDate(@PathVariable("courserId") Long courseId){
        Course course = courseService.getById(courseId);
        System.out.println(courseId);
        return R.ok(course);
    }

    @GetMapping("/pageScoreInfo/{courseId}")
    public R<Page> pageScoreInfo(@PathVariable("courseId") Long courseId,Integer pageNum,Integer pageSize){
        Long id = getUserId();
        LambdaQueryWrapper<ScoreLog> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(courseId!=null,ScoreLog::getCourseId,courseId)
                .eq(ScoreLog::getUserId,id)
                .orderByDesc(ScoreLog::getCreateTime);
        Page<ScoreLog> page =new Page<ScoreLog>(pageNum,pageSize);
        scoreLogService.page(page, lambdaQueryWrapper);
        return R.ok(page);
    }

    @GetMapping("/pageRank/{courseId}")
    public R<Map> pageRank(@PathVariable("courseId") Long courseId,Integer pageNum,Integer pageSize){

        Map<Integer, Student> ranking = rankingUtils.getAll(courseId);
        return R.ok(ranking);
    }
    @GetMapping("/pageClass/{teacherId}")
    public R<Page<Course>> pageClass(@PathVariable("teacherId") Long teacherId, Integer pageNum, Integer pageSize){
        LambdaQueryWrapper<Course> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Course::getTeacherId,teacherId);
        Page<Course> page = new Page<>(pageNum,pageSize);
        courseService.page(page,lambdaQueryWrapper);
        return R.ok(page);
    }






}
